package com.yk;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        test1();
        test2();
        }

       public static void test1() {
           System.out.println("-------------------------------");

           TreeMap<Student, String> treeMap = new TreeMap<>();

           treeMap.put(new Student("赵鑫" , 20),"四川");
           treeMap.put(new Student("王晓明", 22),"广东");
           treeMap.put(new Student("张三" , 23),"河南");
           treeMap.put(new Student("李四" , 21),"湖北");

           treeMap.forEach((k,v) -> System.out.println(k + " : " + v));

       }

       public static void test2() {
        System.out.println("-------------------------------");
        TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
           treeMap.put(new Student("赵鑫" , 20),"四川");
           treeMap.put(new Student("王晓明", 22),"广东");
           treeMap.put(new Student("张三" , 23),"河南");
           treeMap.put(new Student("李四" , 21),"湖北");

           treeMap.forEach((k,v) -> System.out.println(k + " : " + v));

       }

}
